#' Get AQS data from EPA
#'
#' This function downloads and cleans the Air Quality System (AQS) data from EPA
#' for the FIPS code
#' 
#' @param paramter parameter used to name csv file on EPA website: https://aqs.epa.gov/aqsweb/airdata/download_files.html#Raw
#' @param year year for which data is needed
#' @param FIPS_codes: Federal Information Processing Standard code for location
#' @param parameter_codes paramter code for measure of interest: https://aqs.epa.gov/aqsweb/documents/codetables/parameters.html
#' @param save_file True is source file should be saved
#' 

require(tidyverse)

getDailyAQS <- function(parameter, year, FIPS_codes, parameter_codes, save_file = FALSE){
  
  # downloading data file from https://aqs.epa.gov
  
  file_link <- paste("https://aqs.epa.gov/aqsweb/airdata/daily_", parameter, "_", year, ".zip", sep = "")
  
  download.file(file_link, destfile = paste("./aqs_data/daily_", parameter, "_", year, ".zip", sep = ""), method = "libcurl")
  
  zip_file <- paste("./aqs_data/daily_", parameter, "_", year, ".zip", sep = "")
  
  out_dir <-"./aqs_data"
  
  unzip(zip_file,exdir = out_dir)
  
  # creating data set
  
  aqs_data <- data.table::fread(Sys.glob(paste("./aqs_data/?aily_", parameter, "_", year, ".csv", sep = "")), data.table = FALSE) %>%
    rename(state_FIPS = `State Code`,
           county_FIPS = `County Code`,
           site_number = `Site Num`,
           parameter = `Parameter Code`,
           date_local = `Date Local`,
           value = `Arithmetic Mean`) %>%
    mutate(state_FIPS = str_pad(state_FIPS, 2, "0", side = "left"),
           county_FIPS = str_pad(county_FIPS, 3, "0", side = "left"),
           FIPS_code_site = paste(state_FIPS, county_FIPS, "-", site_number, sep = ""),
           value = as.numeric(value)) %>%
    filter(FIPS_code_site %in% FIPS_codes_site) %>% 
    select(state_FIPS, county_FIPS, site_number, parameter, date_local, value) %>%
    filter(!is.na(value) & parameter %in% parameter_codes) %>%
    group_by(state_FIPS, county_FIPS, parameter, date_local) %>% 
    summarise(value = round(mean(value, na.rm = TRUE), digits = 3)) %>% 
    ungroup() %>% 
    mutate_all(as.character) %>% 
    mutate(parameter = ifelse(parameter == "88101", "PM2.5", parameter),
           parameter = ifelse(parameter == "88502", "PM2.5_local", parameter),
           parameter = ifelse(parameter == "42101", "CO", parameter),
           parameter = ifelse(parameter == "44201", "ozone", parameter))
  
  # saving file to data directory
  
  if (save_file){
    unlink("./aqs_data/*.zip")
  }else{
    unlink("./aqs_data/*")
  }
  
  # close file connections
  closeAllConnections()
  
  return(aqs_data)
}
